package com.lanou3g.bookstore.category.admin.web.servlet;

import com.lanou3g.bookstore.category.domain.Category;
import com.lanou3g.bookstore.category.service.CategoryService;
import com.lanou3g.bookstore.util.upload.UU;
import com.lanou3g.bookstore.util.util.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/adminjsps/admin/adminCategory.do")
public class AdminCategoryServlet extends BaseServlet {
    private CategoryService categoryService = new CategoryService();

    private void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Category> bookClass = categoryService.findAll();
        request.setAttribute("bookClass", bookClass);
        request.getRequestDispatcher("/adminjsps/admin/category/list.jsp").forward(request, response);
    }

    private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Category category = new Category();
        String cname = request.getParameter("cname");
        category.setCid(UU.getUUID()).setCname(cname);
        String msg = categoryService.add(category);
        List<Category> bookClass = categoryService.findAll();
//        request.setAttribute("msg", msg);
        request.setAttribute("bookClass", bookClass);
        request.getRequestDispatcher("/adminjsps/admin/category/list.jsp").forward(request, response);
//        request.getRequestDispatcher("/adminjsps/admin/category/add.jsp").forward(request, response);
    }

    private void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String cid = request.getParameter("cid");
        String msg = categoryService.del(cid);
        request.setAttribute("msg", msg);
        request.getRequestDispatcher("/adminjsps/admin/msg.jsp").forward(request, response);
    }

    private void editPre(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String cid = request.getParameter("cid");
        Category category = categoryService.findByCid(cid);
        request.setAttribute("category", category);
        request.getRequestDispatcher("/adminjsps/admin/category/mod.jsp").forward(request, response);
    }

    private void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String cid = request.getParameter("cid");
        String cname = request.getParameter("cname");
        Category category = new Category();
        category.setCid(cid)
                .setCname(cname);
        categoryService.edit(category);
        List<Category> bookClass = categoryService.findAll();
        request.setAttribute("bookClass", bookClass);
        request.getRequestDispatcher("/adminjsps/admin/category/list.jsp").forward(request, response);
    }

}
